package org.presence.dao.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.presence.dao.EtudiantDao;
import org.presence.hibernate.Etudiants;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class EtudiantDaoImpl extends HibernateDaoSupport implements EtudiantDao {

	@Override
	public Etudiants findEtudiantById(Etudiants e) {
		if (null == e) {
			throw new IllegalArgumentException(
					"Login and password are mandatory. Null values are forbidden.");
		}
		try {
			logger.info("Check Etudiant with id: " + e.getId());
			return (Etudiants) getHibernateTemplate().load(Etudiants.class,
					new Integer(e.getId()));
		} catch (DataAccessException ex) {
			// Critical errors : database unreachable, etc.
			logger.error("Exception - DataAccessException occurs : "
					+ ex.getMessage() + " on complete checkLogin().");
		}
		return null;
	}

	@Override
	public List<Etudiants> listEtudiants() {
		try {
			logger.info("Retrieveing etudiants");
			return (List<Etudiants>) getHibernateTemplate()
					.findByCriteria(
							DetachedCriteria.forClass(Etudiants.class)
									.addOrder(Order.asc("nom")));
		} catch (DataAccessException ex) {
			// Critical errors : database unreachable, etc.
			logger.error("Exception - DataAccessException occurs : "
					+ ex.getMessage() + " on complete checkLogin().");
		}
		return null;
	}

	@Override
	public void addEtudiant(Etudiants e) {
		if (null == e) {
			throw new IllegalArgumentException(
					"Login and password are mandatory. Null values are forbidden.");
		}
		try {
			HibernateTemplate template= getHibernateTemplate();
			logger.debug(template);
			template.save(e);
		} catch (DataAccessException ex) {
			// Critical errors : database unreachable, etc.
			logger.error("Exception - DataAccessException occurs : "
					+ ex.getMessage() + " on complete checkLogin().");
		}
	}

	@Override
	public void addEtudiants(List<Etudiants> l) {
		try {
			for (Etudiants e : l) {
				getHibernateTemplate().save(e);
			}
		} catch (DataAccessException ex) {
			// Critical errors : database unreachable, etc.
			logger.error("Exception - DataAccessException occurs : "
					+ ex.getMessage() + " on complete checkLogin().");
		}
	}

	@Override
	public void removeEtudiant(Etudiants e) {
		if (null == e) {
			throw new IllegalArgumentException(
					"Login and password are mandatory. Null values are forbidden.");
		}
		try {
			getHibernateTemplate().delete(e);
		} catch (DataAccessException ex) {
			// Critical errors : database unreachable, etc.
			logger.error("Exception - DataAccessException occurs : "
					+ ex.getMessage() + " on complete checkLogin().");
		}

	}

	@Override
	public void removeEtudiants(List<Etudiants> l) {
		try {
			for (Etudiants e : l) {
				getHibernateTemplate().delete(e);
			}
		} catch (DataAccessException ex) {
			// Critical errors : database unreachable, etc.
			logger.error("Exception - DataAccessException occurs : "
					+ ex.getMessage() + " on complete checkLogin().");
		}

	}

	@Override
	public void updateEtudiant(Etudiants e) {
		if (null == e) {
			throw new IllegalArgumentException(
					"Login and password are mandatory. Null values are forbidden.");
		}
		try {
			getHibernateTemplate().saveOrUpdate(e);
		} catch (DataAccessException ex) {
			// Critical errors : database unreachable, etc.
			logger.error("Exception - DataAccessException occurs : "
					+ ex.getMessage() + " on complete checkLogin().");
		}
	}

	@Override
	public void updateEtudiants(List<Etudiants> l) {
		try {
			for (Etudiants e : l) {
				getHibernateTemplate().saveOrUpdate(e);
			}
		} catch (DataAccessException ex) {
			// Critical errors : database unreachable, etc.
			logger.error("Exception - DataAccessException occurs : "
					+ ex.getMessage() + " on complete checkLogin().");
		}
	}
	
	

}
